package com.cskaoyan.market.service;

import com.cskaoyan.market.db.domain.MarketComment;
import com.cskaoyan.market.db.domain.MarketCommentExample;
import com.cskaoyan.market.db.mapper.MarketCommentMapper;
import com.cskaoyan.market.util.MybatisUtils;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class MarketCommentServiceImpl implements MarketCommentService {

    @Override
    public List<MarketComment> list(Integer page, Integer limit, String userId, String valueId, String sort, String order) {
        //mybatis从数据库中查询数据
        SqlSession session = MybatisUtils.getSession();
        MarketCommentMapper marketCommentMapper = session.getMapper(MarketCommentMapper.class);
        MarketCommentExample marketCommentExample = new MarketCommentExample();
        marketCommentExample.setOrderByClause(sort + " " + order);
        MarketCommentExample.Criteria criteria = marketCommentExample.createCriteria();
        if (!StringUtils.isEmpty(userId)) {
            criteria.andUserIdEqualTo(Integer.parseInt(userId));
        }
        if (!StringUtils.isEmpty(valueId)) {
            criteria.andValueIdEqualTo(Integer.parseInt(valueId));
        }
        PageHelper.startPage(page, limit);
        List<MarketComment> marketCommentList = marketCommentMapper.selectByExample(marketCommentExample);
        session.commit();
        session.close();
        return marketCommentList;
    }

    @Override
    public void delete(MarketComment marketComment) {
        SqlSession session = MybatisUtils.getSession();
        MarketCommentMapper commentMapper = session.getMapper(MarketCommentMapper.class);
        commentMapper.deleteByPrimaryKey(marketComment.getId());
        session.commit();
        session.close();
    }
}

